﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using GiftNet.IDAL;
using GiftNet.DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace GiftNet.SQLServerDAL
{
    #region dbo.ProductPhoto DAL -(by fangyifeng at 2007-09-14 14:37:14)
	public class ProductPhoto:IProductPhoto
	{
		
		/// <summary>
		/// Check exists for PrimaryKey
		/// </summary>
		public bool Exists(Guid ProPhotoID)	
		{
		    try
            {
                NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
                GiftNet.Model.ProductPhoto model = (GiftNet.Model.ProductPhoto)session.Load(typeof(GiftNet.Model.ProductPhoto), ProPhotoID);
                session.Close();

                if (model != null)
                    return true;
                else
                    return false;
            }
            catch
            {
                return false;
            }
		}
		
		/// <summary>
		/// Add record for model
		/// </summary>
		public bool Add(GiftNet.Model.ProductPhoto ProductPhotoModel)
		{
			bool ret = true;
            NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();

            try
            {
                session.Save(ProductPhotoModel);
                // commit all of the changes to the DB and close the ISession
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                ret = false;
            }
            finally
            {
                session.Close();
            }

            return ret;
		}
		
		/// <summary>
		/// Add record for model and return the key object 
		/// </summary>
		public object AddWithReturnKey(GiftNet.Model.ProductPhoto ProductPhotoModel)
		{
		    NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();

            object obj = null;
            try
            {
                obj = session.Save(ProductPhotoModel);
                // commit all of the changes to the DB and close the ISession
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                obj = null;
            }
            finally
            {
                session.Close();
            }

            return obj;
		}
		
		/// <summary>
		/// Update record for model		
		/// </summary>
		public bool Update(GiftNet.Model.ProductPhoto ProductPhotomodel)
		{
			bool ret = true;
            NHibernate.ISession session =  GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();
            try
            {
                session.Update(ProductPhotomodel);

                // commit all of the changes to the DB and close the ISession
                transaction.Commit();
                session.Close();
            }
            catch
            {
                transaction.Rollback();
                ret = false;
            }
            finally
            {
                transaction.Dispose();
                session.Close();
            }

            return ret;
		}
		
		
		/// <summary>
		/// Delete record
		/// </summary>
		public bool Delete(GiftNet.Model.ProductPhoto ProductPhotoModel)
		{
		    try
            {
                NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
                NHibernate.ITransaction transaction = session.BeginTransaction();

                session.Delete(ProductPhotoModel);

                // commit all of the changes to the DB and close the ISession
                transaction.Commit();
                session.Close();

                return true;
            }
            catch
            {
                return false;
            }
		}
		
		
		
		/// <summary>
		/// Delete record for PrimaryKey
		/// </summary>
		public bool Delete(Guid ProPhotoID)
		{
			 bool ret = true;
            NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();

            try
            {
                // Tell NHibernate that this object should be saved
                GiftNet.Model.ProductPhoto model = (GiftNet.Model.ProductPhoto)session.Load(typeof(GiftNet.Model.ProductPhoto), ProPhotoID);
                // 
                session.Delete(model);
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                transaction.Dispose();
                session.Close();

                ret = false;
            }

            return ret;
		}
		
		
		/// <summary>
		/// Get Model fro PrimaryKey
		/// </summary>
        public GiftNet.Model.ProductPhoto GetModel(Guid ProPhotoID)
		{
			NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            NHibernate.ITransaction transaction = session.BeginTransaction();

            GiftNet.Model.ProductPhoto model = (GiftNet.Model.ProductPhoto)session.Load(typeof(GiftNet.Model.ProductPhoto), ProPhotoID);

            transaction.Commit();
            session.Close();

            return model;
		}
	
		
		/// <summary>
        /// Get ModelList fro Where no suppose Page
        /// </summary>
		/// <param name="iNum">return the record's num,IF not need please insert ""</param>
        /// <param name="strWhere">strWhere,need insert KeyWord->WHERE IF you need this Param</param>
		/// <param name="strOrderBy">strOrderBy,need insert KeyWord->Order By IF you need this Param</param>
        /// <returns>IList&lt;GiftNet.Model.ProductPhoto&gt;</returns>
		public IList<GiftNet.Model.ProductPhoto> GetModelList(string strWhere)
		{
			NHibernate.ISession session = GiftNet.DBUtility.DBHelper.SessionFactory().OpenSession();
            return session.CreateQuery(strWhere).List<GiftNet.Model.ProductPhoto>();
		}
		
		
		/// <summary>
        /// Get ModelList fro Where suppose Page
        /// </summary>
        /// <param name="PageSize">PageSize</param>
        /// <param name="PageIndex">PageIndex</param>
        /// <param name="recordCount">recordCount</param>
        /// <param name="strWhere">strWhere</param>
        /// <returns>IList&lt;GiftNet.Model.ProductPhoto&gt;</returns>
		public IList<GiftNet.Model.ProductPhoto> GetModelList(int PageSize, int PageIndex, out int recordCount, string strWhere)
		{
			
			recordCount = 0;
            IList sList = GiftNet.DBUtility.DBHelper.Query(" select productphoto from ProductPhoto productphoto" +((strWhere.Trim().Length<=0)?"":" where "+strWhere), PageIndex, PageSize);
           
            if (sList.Count <= 0)
                return null;
            IList<GiftNet.Model.ProductPhoto> sRetList = new List<GiftNet.Model.ProductPhoto>();
            foreach (object obj in sList)
            {
                sRetList.Add((GiftNet.Model.ProductPhoto)obj);
            }
            recordCount = GiftNet.DBUtility.DBHelper.Query("select count(1) from ProductPhoto productphoto" + ((strWhere.Trim().Length <= 0) ? "" : " where " + strWhere)).Count;
            
            return sRetList;	
		}
	}
	#endregion
}

